/*
 * This file is part of the K-net login application.
 *
 * K-net login application is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 *
 * K-net login is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with the K-net login application.
 * If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Copyright (C) 2011 Ulrik Andersen.
 * Copyright (C) 2011 Kasper Sjørslev.
 */

package dk.knet.login;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class MainService extends Service {
	
	private static final String TAG = "MainService";
	
	public static final boolean LOG = false;
	public static final int NO_CONN = 0, CONN = 1, TRYING=2, HOST_ERROR = 3, CONN_ERROR = 4;
	
	private static int statusCode;
	private static boolean running = false;
	
	private SharedPreferences settings;
	private String host, user, pass;			
	
	private final IBinder mBinder = new MainBinder();
	private NotificationManager nManager;
	private FirewallConnection fwCon;

	@Override
	public void onCreate() {	
		super.onCreate();
		running = true;
		if(MainService.LOG) Log.i(TAG, "onCreate() invoked");
		nManager = (NotificationManager) getSystemService((String) Context.NOTIFICATION_SERVICE);		
	}	
	
    @Override
    public void onDestroy() { 
    	super.onDestroy();
    	running = false;
    	if(MainService.LOG) Log.i(TAG, "onDestroy() invoked");  	
    	nManager.cancel(R.string.local_service_started);	
    	stopService();
    	setStatus(MainService.NO_CONN);
    }	

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	if(MainService.LOG) {
    		Log.i(TAG, "onStartCommand() invoked");
        	Log.i(TAG, "Received start id " + startId + ": " + intent);
    	}
        getSettings();
        startService();
        doNotify(intent);      
        return START_STICKY;
    }

	@Override
	public IBinder onBind(Intent intent) {		
		onStartCommand(intent, 0, 0);	
		if(MainService.LOG) Log.i(TAG, "onBind() invoked");
		return mBinder;
	}	
		 
	public synchronized static int getStatus() {
		return statusCode;
	}
	
	public synchronized static void setStatus(int s) {
		statusCode = s;
	}
	
	public synchronized static boolean getRunning() {
		return running;
	}
	
	private void startService() {
		if(MainService.LOG) Log.i(TAG, "startservice() invoked");			
		fwCon = new FirewallConnection(host,user,pass);
		try {
			fwCon.startThread();
		} 
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void stopService() {
		if(MainService.LOG) Log.i(TAG, "stopservice() invoked");	
		fwCon.disconnect();
		try {
			fwCon.stopThread();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}	
	
    public class MainBinder extends Binder {
    	MainService getService() {
    		return MainService.this;
        }
    }	

	private void getSettings() {
		settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());		
		host = settings.getString("host","fw2.k-net.dk");
		user = settings.getString("user","");
		pass = settings.getString("pass","");
	}
	        
	private void doNotify(Intent intent) {
        CharSequence serviceLabel = getText(R.string.local_service_label);
        CharSequence tickerText = getText(R.string.local_service_started);
        
		Notification notification = new Notification(R.drawable.icon_knet_green, tickerText, System.currentTimeMillis());	
		Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);	
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

		notification.setLatestEventInfo(MainService.this, serviceLabel, tickerText, contentIntent);
		notification.flags |= Notification.FLAG_ONGOING_EVENT;	
		nManager.notify(R.string.local_service_started, notification);
	}
}
